/*© 2010-2011 mOcean Mobile. A subsidiary of Mojiva, Inc. All Rights Reserved.*/
package com.adserver.adview.examples;

import com.adserver.adview.AdServerException;
import com.adserver.adview.AdServerView;
import com.adserver.adview.Logger;
import com.adserver.adview.interfaces.OnAdDownload;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;

class AdServerViewSampleRunner {
    protected AdServerView adServerView;
    protected JTextArea textArea;
    private JScrollPane jScrollPane1;

    // required override method
    protected void initAdServerView() throws AdServerException {
        adServerView = null;
    }

    public void runSample() {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // param for registration
                adServerView.setAdvertiserId("23DR67");
                adServerView.setGroupCode("TR57");
                // events
                adServerView.setAdDownload(new UserAdDownload());

                textArea = new JTextArea();
                textArea.setColumns(23);
                textArea.setLineWrap(true);
                textArea.setRows(7);
                textArea.setWrapStyleWord(true);

                jScrollPane1 = new JScrollPane(textArea);

                JButton updateButton = new JButton("Update banner manually");
                updateButton.addActionListener(new ButtonActionListener());

                // run gui part
                JFrame frame = new JFrame();
                Container contentPane = frame.getContentPane();
                contentPane.setLayout(null);

                ImageIcon imh = new ImageIcon("data\\mOcean2.jpg");
                frame.setSize(imh.getIconWidth(), imh.getIconHeight());

                JPanel panelBgImg = new JPanel() {
                    public void paintComponent(Graphics g) {
                        Image img = new ImageIcon("data\\mOcean2.jpg").getImage();
                        Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
                        setPreferredSize(size);
                        setMinimumSize(size);
                        setMaximumSize(size);
                        setSize(size);
                        setLayout(null);
                        g.drawImage(img, 0, 0, null);
                    }
                };

                contentPane.add(panelBgImg);
                panelBgImg.setBounds(0, 0, imh.getIconWidth(), imh.getIconHeight());

                FlowLayout layout = new FlowLayout();
                JPanel panelContent = new JPanel(layout);

                adServerView.setPreferredSize(new Dimension(imh.getIconWidth() - 30, 100));
                panelContent.add(adServerView);
                panelContent.setBackground(Color.GRAY);
                panelContent.setBorder(new LineBorder(Color.WHITE));

                panelBgImg.add(panelContent);
                panelBgImg.setLayout(new FlowLayout(FlowLayout.LEFT));

                frame.setResizable(false);

                frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                frame.addWindowListener(adServerView.getWindowListener());

                Image icon = Toolkit.getDefaultToolkit().getImage(
                        this.getClass().getClassLoader().getResource("images/mOcean.gif"));
                frame.setIconImage(icon);

                frame.setAlwaysOnTop(true);
                frame.setVisible(true);

                // Get the size of the screen
                Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
                // Determine the new location of the window
                int w = frame.getSize().width;
                int h = frame.getSize().height;
                int x = (dim.width - w) / 2;
                int y = (dim.height - h) / 2;
                // Move the window
                frame.setLocation(x, y);
            }
        });
    }


    class ButtonActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            adServerView.update();
        }
    }

    class UserAdDownload implements OnAdDownload {
        public void begin() {
            String msg = "Begin downloading from com.adserver.adview.examples.AdServerSample5" + "\n";
            Logger.getInstance().logText(Level.INFO, msg);
            textArea.append(msg);
        }

        public void end() {
            String msg = "End downloading from com.adserver.adview.examples.AdServerSample5" + "\n";
            Logger.getInstance().logText(Level.INFO, msg);
            textArea.append(msg);
        }

        public void error(String arg0) {
            String msg = "Error downloading from com.adserver.adview.examples.AdServerSample5: " + arg0 + "\n";
            Logger.getInstance().logText(Level.INFO, msg);
            textArea.append(msg);
        }
    }
}